# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:张广勤
@Web site: https://www.tunan.wang
@Github:www.github.com
 
@File:search_csv1_0.py
@Time:2024/4/28 20:51

@Motto:不积跬步无以至千里，不积小流无以成江海！
"""

import os
import csv

def search_csv_files(directory, search_content):
    search_result=[]
    # 遍历指定目录下的所有文件
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.csv'):
                file_path = os.path.join(root, file)
                with open(file_path, 'r',encoding='utf-8') as csvfile:
                    reader = csv.reader(csvfile)
                    # 遍历CSV文件的每一行
                    for row_num, row in enumerate(reader, start=1):
                        # 将行内容转化为字符串，然后检查是否包含特定内容
                        if search_content in ' '.join(row):
                            # print(f"{row}")
                            search_result.append(row)
                            # print(f"在文件 {file_path} 的第 {row_num} 行找到了内容：{row}")
        return search_result

if __name__ == "__main__":
    # 使用函数，指定目录和要查找的内容
    # search_csv_files('./news/', '山西')
    result=search_csv_files('../news/', '山西')
    print(result)

    for item in result:
        print(type(item))
        print(item)